package cn.edu.sdu.databasedocument.controller;

import cn.edu.sdu.databasedocument.model.DatabaseDocumentModel;
import cn.edu.sdu.databasedocument.util.DataSourceDocumentGeneration;
import cn.smallbun.screw.core.engine.EngineFileType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 数据库文档生成接口
 */
@RequestMapping("/doc")
@RestController
public class DatabaseDocumentController {

    /**
     * 生成数据库文档
     * @param model
     * @return
     */
    @PostMapping("/databaseDocument")
    public void databaseDocument(@RequestBody DatabaseDocumentModel model, HttpServletResponse response){
        FileInputStream is = null;
        BufferedInputStream bis = null;
        try {
            response.reset();
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            String fileSuffix = EngineFileType.valueOf(model.getFileType()).getFileSuffix();
            //返回给用户的文件名,是用户输入的
            String userFileName = model.getFileName() + fileSuffix;
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(userFileName, "UTF-8"));

            OutputStream out = response.getOutputStream();

            //要生成的文件名称,固定为123.xxx
            String fileName = "123" + fileSuffix;
            String filePath = "/";
            //获取系统名称
            String osName = System.getProperty("os.name");
            //Windows操作系统
            if (osName != null && osName.toLowerCase().startsWith("windows")) {
                filePath = System.getProperty("user.home") + "/Desktop/";
                System.out.println(String.format("当前系统版本是:%s", osName));
            } else if (osName != null && osName.toLowerCase().startsWith("linux")) {//Linux操作系统
                System.out.println(String.format("当前系统版本是:%s", osName));
            } else { //其它操作系统
                System.out.println(String.format("当前系统版本是:%s", osName));
            }
            model.setFileOutputDir(filePath);
            model.setFileName("123");
            DataSourceDocumentGeneration.create(model);
            String path = filePath + fileName;
            File file = new File(path);
            is = new FileInputStream(file);
            bis = new BufferedInputStream(is);
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = bis.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.close();
            is.close();
            file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
